﻿Imports Phila
Imports Phila.Sql
Imports Phila.CommonClasses
Imports System.IO
Imports System.Drawing
Imports System.Data

Public Class EditZone
    Inherits System.Web.UI.Page

    Public Shared MAX_THUMBNAIL_WIDTH As Integer = 150
    Public Shared MAX_THUMBNAIL_HEIGHT As Integer = 113

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            'If Request.Cookies("Admin") Is Nothing Then
            If Session.Item("UserID") Is Nothing Then
                'Response.Redirect("Login.aspx")
            End If
            If Not IsPostBack = True Then
                Fill_ComboBox(cmbCategory, "select Id, Name from ptn_Countries")
                additem(cmbCategory, "--Select--", 0)
                If Not Request.QueryString("zid") Is Nothing Then
                    fillData(Request.QueryString("zid"))
                End If
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub fillData(ByVal Id As Integer)
        Dim arr As Array
        arr = RetriveData("select z.Id,z.Name,z.Description,z.Thumbnail,z.MetaTitle,z.MetaKeywords,z.MetaDescription,z.IsActive,z.ViewOrder,cz.countryid from ptn_Zones z  inner join ptn_CatZoneTours cz on cz.ZoneId=z.Id where Id=" & Id, 9)
        txtName.Text = Server.HtmlDecode(arr(1))
        txtDescription.text = Server.HtmlDecode(arr(2))
        ltrImg.Text = "<p><img src=""../images/img/" & arr(3) & """ align=""absmiddle"" /></p>"
        txtMTitle.Text = Server.HtmlDecode(arr(4))
        txtMKeywords.Text = Server.HtmlDecode(arr(5))
        txtMDescription.Text = Server.HtmlDecode(arr(6))
        chkIsActive.Checked = arr(7)
        txtViewOrder.Text = arr(8)
        cmbCategory.SelectedValue = arr(9)
    End Sub

    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Try
            Dim intZoneId As Integer
            Dim chkActive As String
            If chkIsActive.Checked = True Then
                chkActive = "1"
            Else
                chkActive = "0"
            End If
            If txtViewOrder.Text = "" Then
                txtViewOrder.Text = 0
            End If
            If Request.QueryString("zid") Is Nothing Then
                Insert_Update_Delete_Record("INSERT INTO [ptn_Zones]([Name],[Description],[MetaTitle],[MetaKeywords],[MetaDescription],[IsActive],[ViewOrder]) VALUES('" & txtName.Text.Replace("'", "&#039;") & "','" & txtDescription.Text.Replace("'", "&#039;") & "','" & txtMTitle.Text.Replace("'", "&#039;") & "','" & txtMKeywords.Text.Replace("'", "&#039;") & "','" & txtMDescription.Text.Replace("'", "&#039;") & "'," & chkActive & ",'" & txtViewOrder.Text.Replace("'", "&#039;") & "')")
                Dim arr As Array
                arr = RetriveData("Select top 1 Id from ptn_Zones where Name='" & txtName.Text.Replace("'", "&#039;") & "' and MetaTitle='" & txtMTitle.Text.Replace("'", "&#039;") & "' order by Id desc", 0)
                intZoneId = arr(0)
                Insert_Update_Delete_Record("INSERT INTO [ptn_CatZoneTours]([CountryId],[ZoneId],[TourId]) VALUES(" & cmbCategory.SelectedValue & "," & intZoneId & ",0)")
            Else
                Insert_Update_Delete_Record("UPDATE ptn_Zones set [Name]='" & txtName.Text.Replace("'", "&#039;") & "',[Description]='" & txtDescription.Text.Replace("'", "&#039;") & "',[MetaTitle]='" & txtMTitle.Text.Replace("'", "&#039;") & "',[MetaKeywords]='" & txtMKeywords.Text.Replace("'", "&#039;") & "',[MetaDescription]='" & txtMDescription.Text.Replace("'", "&#039;") & "',[IsActive]=" & chkActive & ",[ViewOrder]='" & txtViewOrder.Text.Replace("'", "&#039;") & "' where Id=" & Request.QueryString("zid"))
                intZoneId = Request.QueryString("zid")
                Insert_Update_Delete_Record("UPDATE [ptn_CatZoneTours] set [CountryId]=" & cmbCategory.SelectedValue & ",[TourId]=0 where ZoneId=" & Request.QueryString("zid"))
            End If
            UploadFile("ptn_Zones", "Thumbnail", fileUpload, intZoneId, "/img")
            Response.Redirect("ViewZones.aspx")
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Response.Redirect("ViewZones.aspx")
    End Sub


End Class